MovementHelper = {}
MovementHelper.__index = MovementHelper
 
function MovementHelper.new()
    local instance = {
    }
    
    setmetatable(instance, MovementHelper)
    return instance
end

function MovementHelper:moveUnit(unit, x, y, time)
  if chosenUnit:getType() == "Heavy_Horse" then
    
      if math.random(4) <= 2 then
        audio:playKnightMove1()
      else
        audio:playKnightMove2()
      end
  elseif chosenUnit:getType() == "SwordFighter" then
   if math.random(4) <= 2 then
      audio:playSwordFighterMove1()
    else
      audio:playSwordFighterMove2()
    end
  elseif chosenUnit:getType() == "Archer" then
   if math.random(4) <= 2 then
      audio:playArcherMove1()
    else
      audio:playArcherMove2()
    end
  end
  isUnitMoving = true
  unit:moveLoc(x, y, time)
  move:removeRange(2)
  
  local timer = MOAITimer.new()
  timer:setMode(MOAITimer.NORMAL)
  timer:setSpan(time - 1)
  timer:setListener(MOAITimer.EVENT_TIMER_END_SPAN, function() self:disableUserToClickBetweenWalking() end)
  timer:start()
end

function MovementHelper:disableUserToClickBetweenWalking()
  rangeTurnedOn = false
  userCanClick = true
  isUnitMoving = false
end

function MovementHelper:showRange(range, x1, y1, x2, y2)  
  if range == 1 then
    if rangeTurnedOn == false then
      self:createOneRange(x1, y1, x2, y2)
    end
  elseif range == 2 then
    if rangeTurnedOn == false then
      self:createTwoRange(x1, y1, x2, y2)
    end
  elseif range == 3 then
    if rangeTurnedOn == false then
      self:createThreeRange(x1, y1, x2, y2)
    end
  elseif range == 4 then
    if rangeTurnedOn == false then
      self:createFourRange(x1, y1, x2, y2)
    end
  end
end

function MovementHelper:removeRange(range)
  if range == 1 then
    self:removeOneRangeProps()
  elseif range == 2 then
    self:removeTwoRangeProps()
  elseif range == 3 then
    self:removeThreeRangeProps()
  elseif range == 4 then
    self:removeFourRangeProps()
  end
end

function MovementHelper:createOneRange(x1, y1, x2, y2)
  local prop1 = self:createProp(x1, y1, x2, y2, 64, 0)
  local prop2 = self:createProp(x1, y1, x2, y2, 0 , 64)
  local prop3 = self:createProp(x1, y1, x2, y2, 0 , -64)
  local prop4 = self:createProp(x1, y1, x2, y2, -64 , 0)  
 
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop1)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop2)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop3)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop4)  
end

function MovementHelper:createTwoRange(x1, y1, x2, y2)
  local prop1 = self:createProp(x1, y1, x2, y2, 64,    0)
  local prop2 = self:createProp(x1, y1, x2, y2, 0 ,    128)
  local prop3 = self:createProp(x1, y1, x2, y2, 0 ,    64)
  local prop4 = self:createProp(x1, y1, x2, y2, 128 ,  0)
  local prop5 = self:createProp(x1, y1, x2, y2, 0 ,    -64)
  local prop6 = self:createProp(x1, y1, x2, y2, 0 ,    -128)
  local prop7 = self:createProp(x1, y1, x2, y2, -64 ,  0)  
  local prop8 = self:createProp(x1, y1, x2, y2, -128 , 0)
  local prop9 = self:createProp(x1, y1, x2, y2, 64 ,   64)
  local prop10 = self:createProp(x1, y1, x2, y2, -64,  -64)
  local prop11 = self:createProp(x1, y1, x2, y2, -64,  64)
  local prop12 = self:createProp(x1, y1, x2, y2, 64,  -64)
 
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop1)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop2)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop3)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop4)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop5)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop6)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop7)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop8)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop9)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop10)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop11)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop12)
end

function MovementHelper:removeOneRangeProps()
  for i=table.getn(rangeProps), 1, -1 do
    local prop = rangeProps[i]
    MainLayer:removeProp(prop)
    table.remove(rangeProps, i)
  end
end

function MovementHelper:removeTwoRangeProps()
  for i=table.getn(rangeProps), 1, -1 do
    local prop = rangeProps[i]
    MainLayer:removeProp(prop)
    table.remove(rangeProps, i)
  end
end

function MovementHelper:createThreeRange(x1, y1, x2, y2) 
  local prop1 = self:createProp(x1, y1, x2, y2, 64,    0)
  local prop2 = self:createProp(x1, y1, x2, y2, 0 ,    128)
  local prop3 = self:createProp(x1, y1, x2, y2, 0 ,    64)
  local prop4 = self:createProp(x1, y1, x2, y2, 128 ,  0)
  local prop5 = self:createProp(x1, y1, x2, y2, 0 ,    -64)
  local prop6 = self:createProp(x1, y1, x2, y2, 0 ,    -128)
  local prop7 = self:createProp(x1, y1, x2, y2, -64 ,  0)  
  local prop8 = self:createProp(x1, y1, x2, y2, -128 , 0)
  local prop9 = self:createProp(x1, y1, x2, y2, 64 ,   64)
  local prop10 = self:createProp(x1, y1, x2, y2, -64,  -64)
  local prop11 = self:createProp(x1, y1, x2, y2, -64,  64)
  local prop12 = self:createProp(x1, y1, x2, y2, 64,  -64)
  local prop13 = self:createProp(x1, y1, x2, y2, 64 , 128)
  local prop14 = self:createProp(x1, y1, x2, y2, 64 , -128)
  local prop15 = self:createProp(x1, y1, x2, y2, -64 , 128)
  local prop16 = self:createProp(x1, y1, x2, y2, -64 , -128)
  local prop17 = self:createProp(x1, y1, x2, y2, 0 , -192)
  local prop18 = self:createProp(x1, y1, x2, y2, 0 , 192)
  local prop19 = self:createProp(x1, y1, x2, y2, 192 , 0)
  local prop20 = self:createProp(x1, y1, x2, y2, -192 , 0)
  local prop21 = self:createProp(x1, y1, x2, y2, 128 , 64)
  local prop22 = self:createProp(x1, y1, x2, y2, 128 , -64)
  local prop23 = self:createProp(x1, y1, x2, y2, -128 , -64)
  local prop24 = self:createProp(x1, y1, x2, y2, -128 , 64)
  
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop1)
  table.insert(rangeProps, table.getn(rangeProps) + 1,  prop2)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop3)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop4)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop5)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop6)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop7)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop8)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop9)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop10)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop11)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop12)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop13)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop14)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop15)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop16)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop17)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop18)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop19)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop20)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop21)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop22)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop23)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop24)
end

function MovementHelper:createFourRange(x1, y1, x2, y2) 
  local prop1 = self:createProp(x1, y1, x2, y2, 64,    0)
  local prop2 = self:createProp(x1, y1, x2, y2, 0 ,    128)
  local prop3 = self:createProp(x1, y1, x2, y2, 0 ,    64)
  local prop4 = self:createProp(x1, y1, x2, y2, 128 ,  0)
  local prop5 = self:createProp(x1, y1, x2, y2, 0 ,    -64)
  local prop6 = self:createProp(x1, y1, x2, y2, 0 ,    -128)
  local prop7 = self:createProp(x1, y1, x2, y2, -64 ,  0)  
  local prop8 = self:createProp(x1, y1, x2, y2, -128 , 0)
  local prop9 = self:createProp(x1, y1, x2, y2, 64 ,   64)
  local prop10 = self:createProp(x1, y1, x2, y2, -64,  -64)
  local prop11 = self:createProp(x1, y1, x2, y2, -64,  64)
  local prop12 = self:createProp(x1, y1, x2, y2, 64,  -64)
  local prop13 = self:createProp(x1, y1, x2, y2, 64 , 128)
  local prop14 = self:createProp(x1, y1, x2, y2, 64 , -128)
  local prop15 = self:createProp(x1, y1, x2, y2, -64 , 128)
  local prop16 = self:createProp(x1, y1, x2, y2, -64 , -128)
  local prop17 = self:createProp(x1, y1, x2, y2, 0 , -192)
  local prop18 = self:createProp(x1, y1, x2, y2, 0 , 192)
  local prop19 = self:createProp(x1, y1, x2, y2, 192 , 0)
  local prop20 = self:createProp(x1, y1, x2, y2, -192 , 0)
  local prop21 = self:createProp(x1, y1, x2, y2, 128 , 64)
  local prop22 = self:createProp(x1, y1, x2, y2, 128 , -64)
  local prop23 = self:createProp(x1, y1, x2, y2, -128 , -64)
  local prop24 = self:createProp(x1, y1, x2, y2, -128 , 64)
  local prop25 = self:createProp(x1, y1, x2, y2, -256 , 0)
  local prop26 = self:createProp(x1, y1, x2, y2, 256 , 0)
  local prop27 = self:createProp(x1, y1, x2, y2, 0 , -256)
  local prop28 = self:createProp(x1, y1, x2, y2, 0 , 256)
  local prop29 = self:createProp(x1, y1, x2, y2, -64 , 192)
  local prop30 = self:createProp(x1, y1, x2, y2, -128 , 128)
  local prop31 = self:createProp(x1, y1, x2, y2, -192 , 64)
  local prop32 = self:createProp(x1, y1, x2, y2, -192 , -64)
  local prop33 = self:createProp(x1, y1, x2, y2, 192 , 64)
  local prop34 = self:createProp(x1, y1, x2, y2, 192 , -64)
  local prop35 = self:createProp(x1, y1, x2, y2, -128 , -128)
  local prop36 = self:createProp(x1, y1, x2, y2, -64 , -192)
  local prop37 = self:createProp(x1, y1, x2, y2, 64 , -192)
  local prop38 = self:createProp(x1, y1, x2, y2, 128 , -128)
  local prop39 = self:createProp(x1, y1, x2, y2, 128 , 128)
  local prop40 = self:createProp(x1, y1, x2, y2, 64 , 192)
  
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop1)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop2)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop3)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop4)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop5)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop6)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop7)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop8)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop9)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop10)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop11)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop12)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop13)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop14)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop15)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop16)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop17)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop18)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop19)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop20)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop21)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop22)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop23)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop24)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop25)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop26)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop27)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop28)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop29)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop30)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop31)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop32)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop33)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop34)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop35)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop36)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop37)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop38)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop39)
  table.insert(rangeProps, table.getn(rangeProps) + 1, prop40)
end

function MovementHelper:removeThreeRangeProps()
  for i=table.getn(rangeProps), 1, -1 do
    local prop = rangeProps[i]
    
    MainLayer:removeProp(prop)
    
    table.remove(rangeProps, i)
  end
end

function MovementHelper:removeFourRangeProps()
  for i=table.getn(rangeProps), 1, -1 do
    local prop = rangeProps[i]
    
    MainLayer:removeProp(prop)
    
    table.remove(rangeProps, i)
  end
end

function MovementHelper:createProp(x1, y1, x2, y2, x, y)
  local sprite = MOAIGfxQuad2D.new()
  sprite:setRect(x1 + x, y2 + y, x2 + x, y1 + y)
  sprite:setTexture("resources/graphics/world/Highlight.png")
  
  local prop = MOAIProp2D.new()
  prop:setPriority(100)
  prop:setDeck(sprite)
  prop:setBlendMode(MOAIProp.GL_SRC_ALPHA)

  local xN = (x2 + x)
  local yN = -(y2 + y)
  
  local props = screenActionHelper:getObjWithProps(xN, yN)
  local envo = props[1]
  
  if envo[1] ~= nil then
    if envo[1]:getCanUnitBePlaced() == true then
      MainLayer:insertProp(prop)
    end
  end
  return prop
end

return MovementHelper
